﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlPickIntoryProcess
    {
        /// <summary>
        /// 4.0拣货库存
        /// DataSource:Pickdetail Inventory LotAttribute
        /// </summary>
        public static string GetPickIntory(XmlDocument doc)
        {
            #region 通用变量定义
            string userid = string.Empty;
            string whseid = string.Empty;
            string orderkey = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string udf1 = string.Empty;
            string batch = string.Empty;
            double label_qty = 0;
            string return_status = "OK";
            string strSql = string.Empty;
            List<string> sqllists = new List<string>();

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                App.DBconSet();
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("pickintory");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;
                try
                {
                    userid = nodeSku.SelectSingleNode("userid").InnerText;
                    whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                    orderkey= nodeSku.SelectSingleNode("orderkey").InnerText;
                    master_label = nodeSku.SelectSingleNode("master_label").InnerText;
                    try
                    {
                        string[] s = master_label.Split(new char[] { '|' });
                        if (s.Length > 2)
                        {
                            sku = s[0].ToString();
                            udf1 = s[1].ToString();
                            batch = s[2].ToString();
                            label_qty =double.Parse(s[3].ToString());
                        }
                        else
                        {
                            sku = master_label;
                        }
                    }
                    catch (Exception ex)
                    {
                        App.Log("4.0 拣货库存-PDAXmlPickIntoryProcess.cs-2.0", ex.Message.ToString());
                    }

                    strSql = @"SELECT P.whseid,P.pickdetailkey,P.orderkey,P.line_no,P.lot,P.inv_guid,P.sku,L.udf1,L.lottable01,P.qty,P.lottable10,P.status
                                FROM PickDetail AS P
                                LEFT JOIN Inventory AS I on P.lot = I.lot AND I.qty > 0
                                LEFT JOIN LotAttribute AS L ON P.lot = L.lot ";
                    strSql += " WHERE P.sku='"+sku+ "' AND L.udf1='" + udf1 + "' AND L.lottable01='" + batch+"' AND P.qty="+label_qty+ " AND P.whseid='"+whseid+ "' AND P.orderkey='"+orderkey+ "' AND  P.status='12' AND P.line_no is not null AND P.line_no<>'' AND P.lot is not null AND P.lot<>''";
                    DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dt.Rows.Count <= 0)
                    {
                        return_status = "扫描添加MasterLabel列表失败！！！ 可能原因：1、扫描的Masterlabel信息 "+ master_label + " 已经执行过拣货完成确认，不可重复执行  2、出货单号："+orderkey+" 对应的拣货明细中不存在此笔待确认拣货记录（单据确认后才可执行拣货确认）  3、MasterLabel信息在拣货明细表中不存在(料号+订单+批次+数量 匹配不符)";
                    }
                }
                catch (Exception ex)
                {
                    App.Log("4.0 拣货库存-PDAXmlPickIntoryProcess.cs-2.1", ex.Message.ToString());
                }

                #endregion

                #region 构建XML报文
                XmlNode sku_result = docreturn.CreateElement("pickintory_return");

                XmlElement useridResult = docreturn.CreateElement("userid");
                useridResult.InnerText = userid;
                sku_result.AppendChild(useridResult);

                XmlElement whseidResult = docreturn.CreateElement("whseid");
                whseidResult.InnerText = whseid;
                sku_result.AppendChild(whseidResult);

                XmlElement master_labelResult = docreturn.CreateElement("master_label");
                master_labelResult.InnerText = master_label;
                sku_result.AppendChild(master_labelResult);

                XmlElement orderkeyResult = docreturn.CreateElement("orderkey");
                orderkeyResult.InnerText = orderkey;
                sku_result.AppendChild(orderkeyResult);

                XmlElement skuResult = docreturn.CreateElement("sku");
                skuResult.InnerText = sku;
                sku_result.AppendChild(skuResult);

                XmlElement udf1Result = docreturn.CreateElement("udf1");
                udf1Result.InnerText = udf1;
                sku_result.AppendChild(udf1Result);

                XmlElement batchResult = docreturn.CreateElement("batch");
                batchResult.InnerText = batch;
                sku_result.AppendChild(batchResult);

                XmlElement qtyResult = docreturn.CreateElement("qty");
                qtyResult.InnerText = label_qty.ToString();
                sku_result.AppendChild(qtyResult);

                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                return_statusResult.InnerText = return_status;
                sku_result.AppendChild(return_statusResult);

                rootResult.AppendChild(sku_result);
                #endregion
            }
            catch (Exception ex)
            {
                App.Log("4.0 拣货库存-PDAXmlPickIntoryProcess.cs-2.2", ex.Message.ToString());
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }
    }
}
